import {
	GetPlaylistHotLabel,
	GetPlaylist,
	GetPlaylistAllLabel
} from '@/api/index.js'

//准备actions对象——响应组件中用户的动作
const actions = {
	// 获取热门歌单分类标签
	async GetPlaylistHotLabel(state) {
		let info = await GetPlaylistHotLabel()
		state.commit('setGetPlaylistHotLabel', info['tags'])
		return info['tags']
	},

	// 获取标签下的歌单列表
	async GetPlaylist(state, cat) {
		let info = await GetPlaylist(cat)
		state.commit('setPlaylist', info['playlists'])
	},

	// 获取全部歌单标签
	async GetPlaylistAllLabel(state) {
		let info = await GetPlaylistAllLabel()
		state.commit('setPlaylistAllLabel', info['sub'])
		state.commit('setPlaylistAllTitle', info['categories'])
	}
}
//准备mutations对象——修改state中的数据
const mutations = {
	setGetPlaylistHotLabel(state, obj) {
		state.PlaylistHotLabel = obj
	},

	setPlaylist(state, obj) {
		state.Playlist = obj
	},

	setPlaylistAllLabel(state, obj) {
		state.PlaylistAllLabel = obj
	},

	setPlaylistAllTitle(state, obj) {
		state.PlaylistAllTitle = obj
	}
}
//准备state对象——保存具体的数据
const state = {
	PlaylistHotLabel: [], // 热门歌单分类标签
	Playlist: [], // 分类下的歌单列表
	PlaylistAllLabel: [], // 全部歌单标签
	PlaylistAllTitle: [], // 全部歌单标签的标题
}

const getters = {
	content(state) {
		let content = []
		let PlaylistAllTitle = state.PlaylistAllTitle
		for (let item in PlaylistAllTitle) {
			content.push({
				title: PlaylistAllTitle[item],
				body:[]
			})
		}
		let PlaylistAllLabel = state.PlaylistAllLabel
		for (var i = 0; i < PlaylistAllLabel.length; i++) {
			let item = PlaylistAllLabel[i]
			let category = item['category']
			content[category]['body'].push(item)
		}
		return content
	}
}


export default ({
	state,
	mutations,
	actions,
	getters,
	namespaced: true,
})
